package com.luck.easyfoodbatch.adapter;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.sticky.StickyListHeadersAdapter;
import com.luck.easyfoodbatch.util.Utils;
import com.luck.easyrecyclerview.decoration.RecycleViewDivider;
import com.luck.easyrecyclerview.swipe.MyLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * author：luck
 * project：GoodDays
 * package：gooddays.tongyu.com.gooddays.adapter
 * email：893855882@qq.com
 * data：15/12/31
 */
public class OpenBillingAdapter extends BaseAdapter implements StickyListHeadersAdapter, SectionIndexer {
    private Context context;
    private String mSectionLetters[];
    private List<String> values = new ArrayList<>();
    private List<String> list = new ArrayList<>();

    public OpenBillingAdapter(Context context) {
        super();
        values.add("01月");
        values.add("02月");
        values.add("03月");
        this.context = context;
        mSectionLetters = getSectionLetters();
        System.out.println(mSectionLetters.length);
    }

    private String[] getSectionLetters() {
        String[] letters = new String[values.size()];
        for (int i = 0; i < values.size(); i++) {
            letters[i] = values.get(i);
        }
        return letters;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder = null;
        if (view == null) {
            holder = new Holder();
            view = LayoutInflater.from(context).inflate(R.layout.ac_open_billing, null);
            holder.recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        MyLayoutManager manager = new MyLayoutManager(context);
        holder.recyclerView.setItemAnimator(new DefaultItemAnimator());
        holder.recyclerView.addItemDecoration(new RecycleViewDivider(
                context, LinearLayoutManager.HORIZONTAL, Utils.dip2px(context, 0), context.getResources().getColor(R.color.transparent)));
        holder.recyclerView.setLayoutManager(manager);
        OpenCardItemAdapter adapter = new OpenCardItemAdapter(i);
        holder.recyclerView.setAdapter(adapter);
        return view;
    }

    class Holder {
        RecyclerView recyclerView;
    }


    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        HeaderViewHolder holder;
        if (convertView == null) {
            holder = new HeaderViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.header, parent, false);
            holder.text = (TextView) convertView.findViewById(R.id.text1);
            convertView.setTag(holder);
        } else {
            holder = (HeaderViewHolder) convertView.getTag();
        }
        String head = values.get(position);
        holder.text.setText(head);
        return convertView;
    }

    class HeaderViewHolder {
        TextView text;
    }

    @Override
    public Object[] getSections() {
        return mSectionLetters;
    }

    @Override
    public int getPositionForSection(int i) {
        return mSectionLetters.length;
    }


    @Override
    public int getSectionForPosition(int position) {
        return mSectionLetters.length;
    }


    @Override
    public long getHeaderId(int position) {
        char c = mSectionLetters[position].substring(1, 2).charAt(0);
        return c;
    }
}
